﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using WLong.BLL;

namespace WL_WinForm
{
    public partial class WebEx : DockContent
    {
        public WebEx()
        {
            InitializeComponent();
            Init();
            BindData();
            BindWebCategory();
            BindWebPic();
            BindWebPrice();
        }

        private void Init()
        {
            Category webCategory = new Category();
            DataTable dt = webCategory.GetList("Disabled=0").Tables[0];
            COMMON.ControlCommon.ComboBinds(COMMON.ControlCommon.ConvertToComboList(dt, "CategoryName", "CategoryID"), cmbCategoryID);
        }

        private void BindData()
        {
            WebExample web = new WebExample();
            DataSet ds = web.GetList("Disabled=0");
            gvWebEx.DataSource = ds.Tables[0];
        }

        private void BindWebCategory()
        {
            //网站分类
            Rel_WebCategory webCategory = new Rel_WebCategory();
            DataTable dt = webCategory.GetList("Disabled=0").Tables[0];
            gvWebCategory.DataSource = dt;
        }

        private void BindWebPic()
        {
            Rel_WebPicture webPic = new Rel_WebPicture();
            DataTable dt = webPic.GetList("Disabled=0").Tables[0];
            gvWebPic.DataSource = dt;
        }

        private void BindWebPrice()
        {
            Rel_WebPrice webPicce = new Rel_WebPrice ( ) ;
            DataTable dt = webPicce.GetList("Disabled=0").Tables[0];
            gvWebPirce.DataSource = dt;
        }

        #region 网站实例
        //添加、修改
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!ValidateData())
            {
                COMMON.ControlCommon.ShowMessageBoxError("请输入webcode 、webname、weburl");
                return;
            }
            WebExample web = new WebExample();
            if (txtWebExampleID.Text != "")//修改
            {
                WLong.Model.WebExample mWeb = new WLong.Model.WebExample();
                mWeb.WebExampleID = new Guid(txtWebExampleID.Text);
                mWeb.WebCode = txtWebCode.Text.Trim();
                mWeb.WebName = txtWebName.Text.Trim();
                mWeb.WebState = ckState.Checked ? 1 : 0;
                mWeb.WebUrl = txtUrl.Text.Trim();
                mWeb.WebIntroduction = txtWebIntroduction.Text.Trim();
                mWeb.WebInfomation = txtWebInfomation.Text.Trim();
                mWeb.Disabled = 0;
                mWeb.REC_ModifyTime = DateTime.Now;
                mWeb.REC_ModifyBy = COMMON.Login.loginName;

                if (web.Update(mWeb))
                {
                    ClearControl();
                    BindData();
                    COMMON.ControlCommon.ShowMessageBoxOk("修改成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxOk("修改失败！");
                }

            }
            else //插入
            {
                WLong.Model.WebExample mWeb = new WLong.Model.WebExample();
                mWeb.WebExampleID = Guid.NewGuid();
                mWeb.WebCode = txtWebCode.Text.Trim();
                mWeb.WebName = txtWebName.Text.Trim();
                mWeb.WebState = ckState.Checked ? 1 : 0;
                mWeb.WebUrl = txtUrl.Text.Trim();
                mWeb.WebIntroduction = txtWebIntroduction.Text.Trim();
                mWeb.WebInfomation = txtWebInfomation.Text.Trim();
                mWeb.Disabled = 0;
                mWeb.REC_CreateTime = DateTime.Now;
                mWeb.REC_CreateBy = COMMON.Login.loginName;

                web.Add(mWeb);
                ClearControl();
                BindData();
            }
        }

        private bool ValidateData()
        {
            if (txtWebCode.Text == "" || txtWebName.Text == "" || txtUrl.Text == "")
            {
                return false;
            }
            return true;
        }

        private void ClearControl()
        {
            txtWebExampleID.Text = "";
            txtWebCode.Text = "";
            txtWebName.Text = "";
            txtUrl.Text = "";
            txtWebIntroduction.Text = "";
            txtWebInfomation.Text = "";
            ckState.Checked = false;
        }

        //双击
        private void gvWebEx_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtWebExampleID.Text = gvWebEx.CurrentRow.Cells["WebExampleID"].Value.ToString();
            txtWebCode.Text = gvWebEx.CurrentRow.Cells["WebCode"].Value.ToString();
            txtWebName.Text = gvWebEx.CurrentRow.Cells["WebName"].Value.ToString();
            txtUrl.Text = gvWebEx.CurrentRow.Cells["WebUrl"].Value.ToString();
            txtWebIntroduction.Text = gvWebEx.CurrentRow.Cells["WebIntroduction"].Value.ToString();
            txtWebInfomation.Text = gvWebEx.CurrentRow.Cells["WebInfomation"].Value.ToString();
            if (gvWebEx.CurrentRow.Cells["WebState"].Value.ToString() == "1")
            {
                ckState.Checked = true;
            }
            txtWebID.Text = gvWebEx.CurrentRow.Cells["WebExampleID"].Value.ToString();
            txtWebExampleIDPic.Text = gvWebEx.CurrentRow.Cells["WebExampleID"].Value.ToString();
            txtWebExamplePrice.Text = gvWebEx.CurrentRow.Cells["WebExampleID"].Value.ToString();
        }

        private void gvWebEx_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                WebExample web = new WebExample();
                WLong.Model.WebExample mWeb = new WLong.Model.WebExample();
                mWeb.WebExampleID = new Guid(gvWebEx.CurrentRow.Cells["WebExampleID"].Value.ToString());
                mWeb.WebCode = gvWebEx.CurrentRow.Cells["WebCode"].Value.ToString();
                mWeb.WebName = gvWebEx.CurrentRow.Cells["WebName"].Value.ToString();
                mWeb.WebState = int.Parse(gvWebEx.CurrentRow.Cells["WebState"].Value.ToString());
                mWeb.WebUrl = gvWebEx.CurrentRow.Cells["WebUrl"].Value.ToString();
                mWeb.WebIntroduction = gvWebEx.CurrentRow.Cells["WebIntroduction"].Value.ToString();
                mWeb.WebInfomation = gvWebEx.CurrentRow.Cells["WebInfomation"].Value.ToString();
                mWeb.Disabled = 1;
                mWeb.REC_ModifyTime = DateTime.Now;
                mWeb.REC_ModifyBy = COMMON.Login.loginName;

                if (web.Update(mWeb))
                {
                    BindData();
                    COMMON.ControlCommon.ShowMessageBoxOk("删除成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxError("删除失败！");
                }
            }
        }
        #endregion


        #region 网站分类
        private void btnCategory_Click(object sender, EventArgs e)
        {
            if (txtWebID.Text =="")
            {
                COMMON.ControlCommon.ShowMessageBoxError("请选择网站实例！");
                return;
            }
            if (cmbCategoryID.SelectedValue.ToString() == "-999")
            {
                COMMON.ControlCommon.ShowMessageBoxError("请选择分类！");
                return;
            }
            if (txtWebCategoryId.Text !="")
            {
                Rel_WebCategory webCategory = new Rel_WebCategory();
                WLong.Model.Rel_WebCategory mWebCategory = new WLong.Model.Rel_WebCategory();
                mWebCategory.WebCategoryId = new Guid(txtWebCategoryId.Text);
                mWebCategory.WebExampleID = new Guid(txtWebID.Text);
                mWebCategory.CategoryID = new Guid(cmbCategoryID.SelectedValue.ToString());
                mWebCategory.Disabled = 0;
                mWebCategory.REC_ModifyBy = COMMON.Login.loginName;
                mWebCategory.REC_ModifyTime = DateTime.Now;

                if (webCategory.Update(mWebCategory))
                {
                    ClearWebCategory();
                    BindWebCategory();
                    COMMON.ControlCommon.ShowMessageBoxOk("修改成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxOk("修改失败！");
                }
            }
            else
            {
                Rel_WebCategory webCategory = new Rel_WebCategory();
                WLong.Model.Rel_WebCategory mWebCategory = new WLong.Model.Rel_WebCategory();
                mWebCategory.WebCategoryId = Guid.NewGuid();
                mWebCategory.WebExampleID = new Guid(txtWebID.Text);
                mWebCategory.CategoryID = new Guid(cmbCategoryID.SelectedValue.ToString());
                mWebCategory.Disabled = 0;
                mWebCategory.REC_CreateBy = COMMON.Login.loginName;
                mWebCategory.REC_CreateTime = DateTime.Now;

                webCategory.Add(mWebCategory);
                ClearWebCategory();
                BindWebCategory();
            }
        }

        private void gvWebCategory_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtWebCategoryId.Text = gvWebCategory.CurrentRow.Cells["WebCategoryId"].Value.ToString();
            txtWebID.Text = gvWebCategory.CurrentRow.Cells["WebExampleID"].Value.ToString();
            cmbCategoryID.SelectedValue = gvWebCategory.CurrentRow.Cells["CategoryID"].Value.ToString();
        }

        private void gvWebCategory_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                Rel_WebCategory webCategory = new Rel_WebCategory();
                WLong.Model.Rel_WebCategory mWebCategory = new WLong.Model.Rel_WebCategory();
                mWebCategory.WebCategoryId = new Guid(gvWebCategory.CurrentRow.Cells["WebCategoryId"].Value.ToString());
                mWebCategory.WebExampleID = new Guid(gvWebCategory.CurrentRow.Cells["WebExampleID"].Value.ToString());
                mWebCategory.CategoryID = new Guid(gvWebCategory.CurrentRow.Cells["CategoryID"].Value.ToString());
                mWebCategory.Disabled = 1;
                mWebCategory.REC_ModifyBy = COMMON.Login.loginName;
                mWebCategory.REC_ModifyTime = DateTime.Now;

                if (webCategory.Update(mWebCategory))
                {
                    BindWebCategory();
                    COMMON.ControlCommon.ShowMessageBoxOk("删除成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxOk("删除失败！");
                }
            }
        }

        private void ClearWebCategory()
        {
            txtWebID.Text = "";
            txtWebCategoryId.Text = "";
            cmbCategoryID.SelectedValue = "-999";
        }
        #endregion


        #region 网站图片
        private void btnPic_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
            }
            
        }

        private void btnAddPicture_Click(object sender, EventArgs e)
        {
            if (txtPictureName.Text =="")
            {
                COMMON.ControlCommon.ShowMessageBoxError("请输入图片名称");
                return;
            }
            if (txtWebExampleIDPic.Text == "")
            {
                COMMON.ControlCommon.ShowMessageBoxError("请选择网站实例");
                return;
            }
            BLL.wlBLL.BLL_WebExample webPic = new BLL.wlBLL.BLL_WebExample();
            if (txtWebPictureID.Text != "")
            {
                WLong.Model.Rel_WebPicture mWebPic = new WLong.Model.Rel_WebPicture();
                mWebPic.WebPictureID = new Guid(txtWebPictureID.Text);
                mWebPic.WebExampleID = new Guid(txtWebExampleIDPic.Text);
                mWebPic.PictureName = txtPictureName.Text.Trim();
                mWebPic.Path = txtPath.Text.Trim();
                if (txtSort.Text != "")
                {
                    mWebPic.Sort = int.Parse(txtSort.Text);
                }
                mWebPic.Disabled = 0;
                mWebPic.REC_ModifyBy = COMMON.Login.loginName;
                mWebPic.REC_ModifyTime = DateTime.Now;

                byte[] pic = COMMON.BitmapHelper.BitmapToBytes(new Bitmap(pictureBox1.Image));
                mWebPic.BinaryData = pic;

                int state = webPic.AddWebExample(mWebPic, 1);
                if (state > 0)
                {
                    ClearWebPic();
                    BindWebPic();
                    COMMON.ControlCommon.ShowMessageBoxOk("修改成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxError("修改失败！");
                }
            }
            else
            {
                WLong.Model.Rel_WebPicture mWebPic = new WLong.Model.Rel_WebPicture();
                mWebPic.WebPictureID = Guid.NewGuid();
                mWebPic.WebExampleID = new Guid(txtWebExampleIDPic.Text);
                mWebPic.PictureName = txtPictureName.Text.Trim();
                mWebPic.Path = txtPath.Text.Trim();
                if (txtSort.Text !="")
                {
                    mWebPic.Sort = int.Parse(txtSort.Text);
                }
                mWebPic.Disabled = 0;
                mWebPic.REC_CreateBy = COMMON.Login.loginName;
                mWebPic.REC_CreateTime = DateTime.Now;

                byte[] pic = COMMON.BitmapHelper.BitmapToBytes(new Bitmap(pictureBox1.Image));
                mWebPic.BinaryData = pic;

                int state = webPic.AddWebExample(mWebPic, 0);
                if (state > 0)
                {
                    ClearWebPic();
                    BindWebPic();
                    COMMON.ControlCommon.ShowMessageBoxOk("添加成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxError("添加失败！");
                }
            }
        }

        private void ClearWebPic()
        {
            txtWebPictureID.Text = "";
            txtWebExampleIDPic.Text = "";
            txtPictureName.Text = "";
            txtPath.Text = "";
            pictureBox1.Image = null;
            txtSort.Text = "";
        }

        private void gvWebPic_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtWebPictureID.Text = gvWebPic.CurrentRow.Cells["WebPictureID"].Value.ToString();
            txtWebExampleIDPic.Text = gvWebPic.CurrentRow.Cells["WebExampleID"].Value.ToString();
            txtPictureName.Text = gvWebPic.CurrentRow.Cells["PictureName"].Value.ToString();
            txtPath.Text = gvWebPic.CurrentRow.Cells["Path"].Value.ToString();
            txtSort.Text = gvWebPic.CurrentRow.Cells["Sort"].Value.ToString();
            pictureBox1.Image = COMMON.BitmapHelper.BytesToBitmap((byte[])gvWebPic.CurrentRow.Cells["BinaryData"].Value);
        }

        private void gvWebPic_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                BLL.wlBLL.BLL_WebExample webPic = new BLL.wlBLL.BLL_WebExample();
                WLong.Model.Rel_WebPicture mWebPic = new WLong.Model.Rel_WebPicture();
                mWebPic.WebPictureID = new Guid(gvWebPic.CurrentRow.Cells["WebPictureID"].Value.ToString());
                mWebPic.Disabled = 1;
                mWebPic.REC_ModifyBy = COMMON.Login.loginName;
                mWebPic.REC_ModifyTime = DateTime.Now;

                int state = webPic.AddWebExample(mWebPic, 2);
                if (state > 0)
                {
                    BindWebPic();
                    COMMON.ControlCommon.ShowMessageBoxOk("删除成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxOk("删除失败！");
                }
            }
        }
        #endregion

        
        #region 网站价格
        private void button1_Click(object sender, EventArgs e)
        {
            if (txtWebExamplePrice.Text == "")
            {
                COMMON.ControlCommon.ShowMessageBoxError("请选择网站实例");
                return;
            }
            if (txtWebPrice.Text =="")
            {
                COMMON.ControlCommon.ShowMessageBoxError("请输入网站价格");
                return;
            }
            Rel_WebPrice webPrice = new Rel_WebPrice();
            if (txtWebPriceID .Text != "")
            {
                WLong.Model.Rel_WebPrice mWebPrice = new WLong.Model.Rel_WebPrice();
                mWebPrice.WebPriceID = new Guid(txtWebPriceID.Text);
                mWebPrice.WebExampleID = new Guid(txtWebExamplePrice.Text);
                mWebPrice.WebPrice = decimal.Parse(txtWebPrice.Text.Trim());
                if (txtCustomerType.Text !="")
                {
                    mWebPrice.CustomerType = int.Parse(txtCustomerType.Text.Trim());
                }
                mWebPrice.Disabled = 0;
                mWebPrice.REC_ModifyBy = COMMON.Login.loginName;
                mWebPrice.REC_ModifyTime = DateTime.Now;

                if (webPrice.Update(mWebPrice))
                {
                    ClearWebPrice();
                    BindWebPrice();
                    COMMON.ControlCommon.ShowMessageBoxOk("修改成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxError("修改失败！");
                }
            }
            else
            {
                WLong.Model.Rel_WebPrice mWebPrice = new WLong.Model.Rel_WebPrice();
                mWebPrice.WebPriceID = Guid.NewGuid();
                mWebPrice.WebExampleID = new Guid(txtWebExamplePrice.Text);
                mWebPrice.WebPrice = decimal.Parse(txtWebPrice.Text.Trim());
                if (txtCustomerType.Text != "")
                {
                    mWebPrice.CustomerType = int.Parse(txtCustomerType.Text.Trim());
                }
                mWebPrice.Disabled = 0;
                mWebPrice.REC_CreateBy = COMMON.Login.loginName;
                mWebPrice.REC_CreateTime = DateTime.Now;

                webPrice.Add(mWebPrice);
                ClearWebPrice();
                BindWebPrice();
            }
        }

        private void ClearWebPrice()
        {
            txtWebPriceID.Text = "";
            txtWebExamplePrice.Text = "";
            txtWebPrice.Text = "";
            txtCustomerType.Text = "";
        }

        private void gvWebPirce_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtWebPriceID.Text = gvWebPirce.CurrentRow.Cells["WebPriceID"].Value.ToString();
            txtWebExamplePrice.Text = gvWebPirce.CurrentRow.Cells["WebExampleID"].Value.ToString();
            txtWebPrice.Text = gvWebPirce.CurrentRow.Cells["WebPrice"].Value.ToString();
            txtCustomerType.Text = gvWebPirce.CurrentRow.Cells["CustomerType"].Value.ToString();
        }

        private void gvWebPirce_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                Rel_WebPrice webPrice = new Rel_WebPrice();
                WLong.Model.Rel_WebPrice mWebPrice = new WLong.Model.Rel_WebPrice();
                mWebPrice.WebPriceID = new Guid(gvWebPirce.CurrentRow.Cells["WebPriceID"].Value.ToString());
                mWebPrice.WebExampleID = new Guid(gvWebPirce.CurrentRow.Cells["WebExampleID"].Value.ToString());
                mWebPrice.WebPrice = decimal.Parse(gvWebPirce.CurrentRow.Cells["WebPrice"].Value.ToString());
                if (gvWebPirce.CurrentRow.Cells["CustomerType"].Value.ToString() != "")
                {
                    mWebPrice.CustomerType = int.Parse(gvWebPirce.CurrentRow.Cells["CustomerType"].Value.ToString());
                }
                mWebPrice.Disabled = 1;
                mWebPrice.REC_ModifyBy = COMMON.Login.loginName;
                mWebPrice.REC_ModifyTime = DateTime.Now;

                if (webPrice.Update(mWebPrice))
                {
                    BindWebPrice();
                    COMMON.ControlCommon.ShowMessageBoxOk("删除成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxError("删除失败！");
                }
            }
            
        }
        #endregion
    }
}
